from Bug import Bug

class Ranker:
    
    MIN_MATCHING_VALUE = 50
    
    # constructor
    def __init__(self):
        # list of all known bugs
        self.__bugList = []
       
    # add a new cause-effect-chain to an maintained bug
    # or when no matching bug was found, create a new bug    
    def addCEChain(self, chainToAdd):
        # init vars
        maxMatchedValue, maxMatchedBug = -1, None
        # get the highest possible matchingValue and the corresponding bug
        for bug in self.__bugList:
            value = bug.getMatchingValue(chainToAdd)
            if (maxMatchedValue < value):
                (maxMatchedValue, maxMatchedBug) = (value, bug)
        # no bug or lower end wasn't reached
        if (maxMatchedBug == None) or (maxMatchedValue < self.MIN_MATCHING_VALUE):
            # create a new bug object
            maxMatchedBug = Bug()
            self.__bugList.append(maxMatchedBug)
        # add the chain to the found bug
        maxMatchedBug.addNewCEChain(chainToAdd)
        
    # returns a list with the bugs ordered by the bug's number of cause-effect-chains
    def getOrderedBugList(self):
        # init vars
        lst = []
        buglst = self.__bugList
        # sort the Buglst
        while len(buglst) > 0:
            # get the remaining bug with the highest number of related CE-Chains
            (maxSize, maxBug) = (-1, None)
            for bug in self.__bugList:
                size = bug.size()
                if (maxSize < size):
                    (maxSize, maxBug) = (size, bug)
            # remove the bug from the buglst
            buglst.remove(maxBug)
            # add the bug to the lst
            lst.append(maxBug)
        # returns the lst
        return lst
